<%= render partial: 'spree/admin/users/sidebar', locals: { current: :items } %>
<%= render partial: 'spree/admin/users/user_page_actions' %>

<% content_for :page_title do %>
  <%= link_to @user.email, spree.edit_admin_user_url(@user) %> /
  <%= Spree.t(:"admin.user.items_purchased") %>
<% end %>

<fieldset data-hook="admin_user_items_purchased">
  <%= paginate @orders %>

  <% if @orders.any? %>
    <%# TODO add search interface %>
    <table class="table table-condensed table-bordered stock-contents" id="listing_items" data-hook="stock-contents">
      <thead>
        <th><%= sort_link @search, :completed_at, I18n.t(:completed_at, scope: 'activerecord.attributes.spree/order'), {}, {title: 'orders_completed_at_title'} %></th>
        <th colspan=2><%= Spree.t(:description) %></th>
        <th><%= I18n.t(:price, scope: 'activerecord.attributes.spree/line_item') %></th>
        <th><%= I18n.t(:quantity, scope: 'activerecord.attributes.spree/line_item') %></th>
        <th><%= Spree.t(:total) %></th>
        <th><%= sort_link @search, :state, I18n.t(:state, scope: 'activerecord.attributes.spree/order'), {}, {title: 'orders_state_title'} %></th>
        <th><%= sort_link @search, :number, Spree.t(:order_num, scope: 'admin.user'), {}, {title: 'orders_number_title'} %></th>
      </thead>
      <tbody>
        <% @orders.each do |order| %>
          <% order.line_items.each do |item| %>
            <tr class="stock-item" data-item-quantity="<%= item.quantity %>">
              <td class="order-completed-at"><%= order_time(order.completed_at) if order.completed_at %></td>
              <td class="item-image">
                <%= mini_image(item.variant) %>
              </td>
              <td class="item-name">
                <%= item.name %><br><%= "(" + variant_options(item.variant) + ")" unless item.variant.option_values.empty? %>
                <% if item.sku.present? %>
                  <strong><%= Spree.t(:sku) %>:</strong> <%= item.variant.sku %>
                <% end %>
              </td>
              <td class="item-price"><%= item.single_money.to_html %></td>
              <td class="item-quantity"><%= item.quantity %></td>
              <td class="item-total"><%= item.money.to_html %></td>
              <td class="order-state">
                <div class="state <%= order.state.downcase %>"><%= Spree.t("order_state.#{order.state.downcase}") %></div>
                <% if order.payment_state %>
                  <div class="state <%= order.payment_state %>"><%= link_to Spree.t("payment_states.#{order.payment_state}"), admin_order_payments_path(order) %></div>
                <% end %>
                <% if Spree::Order.checkout_step_names.include?(:delivery) && order.shipment_state %>
                  <div class="state <%= order.shipment_state %>"><%= Spree.t("shipment_states.#{order.shipment_state}") %></div>
                <% end %>
              </td>
              <td class="order-number"><%= link_to order.number, edit_admin_order_url(order) %></td>
            </tr>
          <% end %>
        <% end %>
    </table>
  <% else %>
    <div class="alert alert-info no-objects-found">
      <%= Spree.t(:no_resource_found, resource: plural_resource_name(Spree::Order)) %>,
      <%= link_to Spree.t(:add_one), spree.new_admin_order_path %>!
    </div>
  <% end %>
  <%= paginate @orders %>
</fieldset>

<%= render 'spree/admin/users/lifetime_stats' %>
